package com.hoopchina.ui.download;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.os.Handler;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import com.hoopchina.news.R;
import com.hoopchina.ui.main.*;
import com.hoopchina.util.LogUtils;
import com.hoopchina.util.download.DownloadMgr;
import com.hoopchina.util.download.DownloadQueue;
import com.hoopchina.util.download.DownloadMgr.OnDownloadUpdateListener;

public class DownloadActivity extends BaseActivity {
    public static final String      Id           = "DownloadActivity";
    private DownloadMgr             mDownloadMgr = DownloadMgr.getInstance();
    private ViewManager             mViewManager;
    private DownloadListAdaptor     mListAdapter;
    private ArrayList<DownloadItem> mListData;
    private ListView                mListView;

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        LogUtils.d( "in" );
        super.onCreate( savedInstanceState );
        setContentView( R.layout.downloadview );
        mViewManager = ViewManager.getInstance();
        mListView = ( ListView ) findViewById( R.id.downloadview_list );
        mListData = new ArrayList<DownloadItem>();
        LogUtils.d( "out" );
    }

    @Override
    protected void onPause() {
        LogUtils.d( "in" );
        super.onPause();
        LogUtils.d( "out" );
    }

    @Override
    protected void onDestroy() {
        LogUtils.d( "in" );
        super.onDestroy();
        LogUtils.d( "out" );
    }

    private void cancel( int position ) {
        LogUtils.d( "in" );
        if ( position >= mListData.size() || position < 0 ) {
            return;
        }
        mDownloadMgr.cancel( position );
        if ( mListAdapter != null || mListData != null ) {
            mListData.remove( position );
            mListAdapter.notifyDataSetChanged();
        }
        autoBack();
        LogUtils.d( "out" );
    }

    private void cancelAll() {
        LogUtils.d( "in" );
        mDownloadMgr.cancelAll();
        if ( mListAdapter != null || mListData != null ) {
            mListData.clear();
            mListAdapter.notifyDataSetChanged();
        }
        autoBack();
        LogUtils.d( "out" );
    }

    private void initFields() {
        LogUtils.d( "in" );
        List<DownloadQueue> queue = mDownloadMgr.getQueue();
        DownloadItem aNewItem = null;
        mListData.clear();
        for ( int i = 0; i < queue.size(); i++ ) {
            aNewItem = new DownloadItem( queue.get( i ).getFinishedSize(),queue.get( i ).getQueueSize(), queue.get( i ).getType() );
            mListData.add( aNewItem );
        }
        mListAdapter = new DownloadListAdaptor( this, mListData );

        mListView.setOnCreateContextMenuListener( new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu( ContextMenu menu, View v, ContextMenuInfo menuInfo ) {

                LogUtils.d( "onCreateContextMenu()" );
                menu.setHeaderTitle( getString( R.string.app_menu_offline_download ) );
                menu.add( 0, 0, 0, getString( R.string.app_offlinedown_cancel ) );
            }
        } );

        mListView.setAdapter( mListAdapter );

        mDownloadMgr.setDownloadUpdateListener( new OnDownloadUpdateListener() {

            @Override
            public void onProgressUpdate( int done, int total, String type ) {
                LogUtils.d( "done = " + done + "total = " + total );
                if ( mListData == null || mListAdapter == null || mListData.size() == 0 ) {
                    return;
                }
                mListData.get( 0 ).setDoneNum( done );
                mListData.get( 0 ).setTotalNum( total );
                mListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onDownloadFinished() {
                if ( mListData == null || mListData.size() == 0 )
                    return;
                mListData.remove( 0 );
                mListAdapter.notifyDataSetChanged();
                autoBack();
            }
        } );
        LogUtils.d( "out" );
    }

    private void autoBack() {
        LogUtils.d( "in" );
        if ( mListData != null && mListData.size() == 0 ) {
            new Handler().post( new Runnable() {

                @Override
                public void run() {
                    onKeyDown( KeyEvent.KEYCODE_BACK, null );
                }
            } );
        }
        LogUtils.d( "out" );
    }

    @Override
    public boolean onContextItemSelected( MenuItem item ) {
        LogUtils.d( "in" );
        if ( item == null || item.getMenuInfo() == null ) {
            return false;
        }

        AdapterContextMenuInfo info = ( AdapterContextMenuInfo ) item.getMenuInfo();
        LogUtils.d( "info.position = " + info.position );
        switch ( item.getItemId() ) {
            case 0:
            this.cancel( info.position );
                break;

            default:
            break;
    }
    LogUtils.d( "out" );
    return super.onContextItemSelected( item );
}

    @Override
    public void onResume() {
        LogUtils.d( "in" );
        super.onResume();
        mViewManager.setTitle( getString( R.string.app_menu_offline_download ) );
        mViewManager.hideLeftButton();
        mViewManager.setStatus( "" );
        mViewManager.setTabBarVisibility( false );
        mMyHandleBackButton = true;
        initFields();
        LogUtils.d( "out" );
    }

    @Override
    public boolean onOptionsItemSelected( MenuItem item ) {
        LogUtils.d( "in" );
        switch ( item.getItemId() ) {
            case R.id.menu_downloadview_cancel:
            this.cancelAll();
                break;

            default:
            break;
    }
    LogUtils.d( "out" );
    return false;
}

    @Override
    public boolean onPrepareOptionsMenu( Menu menu ) {
        LogUtils.d( "in" );
        menu.clear();
        MenuInflater inflater = getMenuInflater();
        inflater.inflate( R.menu.downloadview, menu );
        LogUtils.d( "out" );
        return true;
    }

}
